package com.base.helper;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

import com.base.comm.CommUrls;
import com.base.po.param.JH;
import com.base.po.voip.Users;
import com.base.utils.GsonUtil;

import com.base.utils.Redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;

/**
 * 久话接口帮助类
 *
 * @author Administrator
 *
 */
@Component
public class JHHelper {

	private final RedisUtil redisUtil;

	public JHHelper(RedisUtil redisUtil) {
		this.redisUtil = redisUtil;
	}

	public static BigDecimal JH = new BigDecimal("0.09");

	/**
	 * 本地获取TOKEN 调用运营商授权
	 *
	 * @return
	 */
	public String getRedis() {
//		@SuppressWarnings("resource")
//		Jedis jedis = new Jedis(CommUrls.REDIS_HOST); // 连接到Redis服务器
//		jedis.auth(CommUrls.REDIS_POSSWORD); // 设置密码
		String token = redisUtil.get(CommUrls.REDIS_JH_TOKEN);
		System.out.println("直接获取redis token：" + token);
		if (token == null) {
			return getToken();
		}
		return token;
	}

	public String getRedisNew() {
//		@SuppressWarnings("resource")
//		Jedis jedis = new Jedis(CommUrls.REDIS_HOST); // 连接到Redis服务器
//		jedis.auth(CommUrls.REDIS_POSSWORD); // 设置密码
		String token = redisUtil.get(CommUrls.REDIS_JH_TOKEN_NEW);
		System.out.println("直接获取redis token：" + token);
		if (token == null) {
			return getTokenNew();
		}
		return token;
	}

	public String getRedisCustom(Users users) {
//		@SuppressWarnings("resource")
//		Jedis jedis = new Jedis(CommUrls.REDIS_HOST); // 连接到Redis服务器
//		jedis.auth(CommUrls.REDIS_POSSWORD); // 设置密码
		String token = redisUtil.get(CommUrls.REDIS_JH_TOKEN_CUSTOM);
		System.out.println("直接获取redis token：" + token);
		if (token == null) {
			return getTokenCustom(users);
		}
		return token;
	}

	/**
	 * 从久话服务端获取TOKEN 调用运营商授权
	 *
	 * @return
	 */
	@SuppressWarnings("resource")
	public String getToken() {
		String tokendata = "{\"Username\":\"20151\",\"Password\":\"B6dfOt3GaNuouyt8mBrZ1BTQqoIQl02A\"}";
		String tokenresult = WebHelper.PostData("http://118.31.238.194:8088/api/login", tokendata, "application/json",
				null);
		System.out.println(tokenresult);
		JH jh = GsonUtil.getObject(tokenresult, JH.class);
//		Jedis jedis = new Jedis(CommUrls.REDIS_HOST); // 连接到Redis服务器
		redisUtil.set(CommUrls.REDIS_JH_TOKEN, jh.getToken());// 更新token
		System.out.println("更新redis token：" + jh.getToken());
		return jh.getToken();
	}

	public String getTokenNew() {
		String tokendata = "{\"Username\":\"8013\",\"Password\":\"EYUu0PKZyrM269uVddL2RfNUmd3Z110V\"}";
		String tokenresult = WebHelper.PostData("http://47.99.119.56:8088/api/login", tokendata, "application/json",
				null);
		System.out.println(tokenresult);
		JH jh = GsonUtil.getObject(tokenresult, JH.class);
//		Jedis jedis = new Jedis(CommUrls.REDIS_HOST); // 连接到Redis服务器
		redisUtil.set(CommUrls.REDIS_JH_TOKEN_NEW, jh.getToken());// 更新token
		System.out.println("更新redis token：" + jh.getToken());
		return jh.getToken();
	}

	public String getTokenCustom(Users users) {
		String tokendata = "{\"Username\":\"" + users.getJhappid() + "\",\"Password\":\"" + users.getJhappkey() + "\"}";
		String tokenresult = WebHelper.PostData("http://47.99.119.56:8088/api/login", tokendata, "application/json",
				null);
		System.out.println(tokenresult);
		JH jh = GsonUtil.getObject(tokenresult, JH.class);
//		Jedis jedis = new Jedis(CommUrls.REDIS_HOST); // 连接到Redis服务器
		redisUtil.set(CommUrls.REDIS_JH_TOKEN_CUSTOM, jh.getToken());// 更新token
		System.out.println("更新redis token：" + jh.getToken());
		return jh.getToken();
	}

	/**
	 * 拨打电话 (久话)
	 *
	 * @param caller 主叫
	 * @param callee164 被叫
	 * @param token
	 * @param id 随机UUID
	 * @return
	 */
	public String bh(String caller, String callee164, String token, String id) {
		String datas = "{\"Callid\":\"" + id
				+ "\",\"App_id\":\"20151\",\"Appkey\":\"B6dfOt3GaNuouyt8mBrZ1BTQqoIQl02A\",\"Caller\":\"" + caller
				+ "\",\"Callee\":\"" + callee164 + "\",\"Cdr_url\":\"" + CommUrls.THISSERVER + "call/JHpush" + "\"}";
		String callresult = WebHelper.PostData("http://118.31.238.194:8088/api/CallRequest", datas,
				"application/json", token);
		System.out.println(callresult);
		return callresult;
	}

	public String bhNew(String caller, String callee164, String token, String id) {
		String datas = "{\"Callid\":\"" + id
				+ "\",\"App_id\":\"8013\",\"Appkey\":\"EYUu0PKZyrM269uVddL2RfNUmd3Z110V\",\"Caller\":\"" + caller
				+ "\",\"Callee\":\"" + callee164 + "\",\"Cdr_url\":\"" + CommUrls.THISSERVER + "call/JHpush" + "\"}";
		String callresult = WebHelper.PostData("http://47.99.119.56:8088/api/CallRequest", datas,
				"application/json", token);
		System.out.println(callresult);
		return callresult;
	}

	public String bhCustom(Users users, String caller, String callee164, String token, String id, String ip) {
		String datas = "{\"Callid\":\"" + id + "appIp=" + ip
				+ "\",\"App_id\":\"" + users.getJhappid() + "\",\"Appkey\":\"" + users.getJhappkey() + "\",\"Caller\":\"" + caller
				+ "\",\"Callee\":\"" + callee164 + "\",\"Cdr_url\":\"" + CommUrls.THISSERVER + "call/JHpush" + "\"}";
		String callresult = WebHelper.PostData("http://47.99.119.56:8088/api/CallRequest", datas,
				"application/json", token);
		System.out.println(callresult);
		return callresult;
	}

	/**
	 * 拨打电话（魅逸）
	 *
	 * @param caller 主叫
	 * @param callee164 被叫
	 * @param id
	 * @param time
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String bhMy(String caller, String callee164, String id, int time) throws UnsupportedEncodingException {
		String callresult = WebHelper.GetData("http://api2.gz-tx.com:2015/index.html?zj=" + caller + "&bj=" + callee164
				+ "&calltime=" + time + "&pwd=c8837b23ff8aaa8a2dde915473ce0991&name=gztx695&params=" + id + "", null);
		return callresult;
	}

}
